var app = getApp()
var gb = app.globalData
var Bmob = gb.Bmob
var SessionMgr = gb.SessionMgr
var Define = require("../../lib/Define.js")
var _styles = []

Page({
	data: {
		s_width: app.globalData.screen.width,
		s_half_width: 0,
		item_img_width: 0,
		wares: [],
    styles:[],
    isLoading:true
	},
	onLoad: function(options) {
		// 生命周期函数--监听页面加载
		this.setData({
			s_half_width: app.globalData.screen.width / 2,
			item_img_width: (app.globalData.screen.width - 20)
		})
	},
	onReady: function() {
		// 生命周期函数--监听页面初次渲染完成
		this.onLoadList();
		this.requestStyle()
	},
	onShow: function() {
		// 生命周期函数--监听页面显示

	},
	requestStyle:function(){
    //请求样式
		var self = this

    wx.request({
      url: Define.value.baseUrl+'/app/ware/stylelist',
      data: {bid:Define.value.bid},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        if(res.data.code == 200){
					console.log(res.data)
					_styles = []
					_styles.push({
						isSelected:true,
						title:"全部"
					})
					for(var i = 0 ; i < res.data.data.length ;i++){
						_styles.push(res.data.data[i])
					}
					self.setData({
						styles:_styles
					})
				}
        
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
	onLoadList: function(code) {
		//加载列表
		var self = this

		var data = {
			bid:Define.value.bid
		}

		if(code){
			data.styleCode = code
		}
		wx.request({
		  url: Define.value.baseUrl+'/app/ware/list',
		  data: data,
		  method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
		  // header: {}, // 设置请求的 header
		  success: function(res){
			// success
			if(res.data.code == 200){
				var wares = []
				var data = res.data.data
				for(var index = 0 ; index < data.length; index++){
					var item = data[index]
					item.mainImage = item.images[0].url
					item.uDateStr = Define.dateFrom(item.uDate,"yyyy-MM-dd")
					wares.push(item)
				}

				self.setData({
					wares:wares,
          isLoading:false
				})

				console.log(data)
			}
		  },
		  fail: function() {
			// fail
		  },
		  complete: function() {
			// complete
		  }
		})
	},
	onCategoryClick:function(event){
		var code = event.target.dataset.code
		console.log(code)
		this.onLoadList(code)

		for(var i = 0 ; i < _styles.length; i++){
			var item = _styles[i]
			item.isSelected = (code == item.code)
		}

		this.setData({
			styles:_styles
		})
	},
  onShareAppMessage: function () {
    return {
      title: Define.loadBusinessInfo().name,
      path: "/pages/ware/ware"
    }
  },
  onDIYOfferButtonClick: function () {
    wx.navigateTo({
      url: '../diyOffer/diyOffer',
    })
  }
})